import {codeReq, loginReq,registerReq, user_detailReq} from "@/api/user.js";
import { baseUrl } from "../../utils/request";
export const user={
	namespaced: true,
	state:{
		data:{
			info:uni.getStorageSync('info')?uni.getStorageSync('info'):null,
			token:uni.getStorageSync('token')?uni.getStorageSync('token'):'',
			detailed_info:uni.getStorageSync('detailed_info')?uni.getStorageSync('detailed_info'):null,
		},
		curHeader:uni.getStorageSync('curHeader')?uni.getStorageSync('curHeader'):null,
	},
	mutations:{
		// 存用户信息/token
		M_data(state,payload){
			const {info,token}=payload
			state.data.info=info
			state.data.token=token
		},
		// 头像修改
		M_curHeader(state,payload){
			uni.setStorageSync('curHeader',baseUrl+payload)
			state.curHeader=baseUrl+payload
		},
		// 用户详细详细修改
		M_detailed_info(state,payload){
			state.detailed_info=payload
		},
	},
	actions:{
		// 登陆
		async A_login(ctx,payload){
			let res = await loginReq(payload)
			const {code,data}=res.data
			if(code==0){
				uni.showToast({
					title: '登陆成功',
					icon:"success"
				});
				uni.switchTab({
					url: '/pages/index/index'
				});
				uni.setStorageSync('info',res.data.data.info)
				uni.setStorageSync('token',res.data.data.token)
				/* localStorage.setItem('info',JSON.stringify((res.data.data.info)))
				localStorage.setItem('token',res.data.data.token) */
				ctx.commit('M_data',res.data.data)
			}else{
				uni.showToast({
					title: res.data.errors,
					icon:"error"
				});
			}
		},
		// 注册
		async A_reg(ctx,payload){
			let res = await registerReq(payload)
			const {code,errors}=res.data
			if(code==0){
				uni.showToast({
					title: '注册成功',
					icon:"success"
				});
				return 'login'
			}else{
				uni.showToast({
					title: errors,
					icon:"error"
				});
				return 'reg'
			}
		},
		async A_detailed_info(ctx,payload){
			let res=await user_detailReq(payload)
			uni.setStorageSync('detailed_info',res.data.data)
			ctx.commit('M_detailed_info',res.data.data)
		}
	}
}
export default user
